// pages/pro_detail_share/pro_detail_share.js
var common = require("../../../common/common.js").common;

var leader = require("../../../common/leader.js").leader;

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        s_leader: [],
        myleaderinfo: {
            leader_id: 1,
            leader_id_true: -1
        }
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad: function(options) {
        this.setData({
            domain: app.globalData.domain.slice(0, -10)
        });
        wx.hideShareMenu();
        let that = this;
        wx.getSystemInfo({
            success: function(res) {
                let rpx = res.windowWidth / 375;
                that.setData({
                    rpx: rpx
                });
            }
        });
        if (options.details) {
            that.setData({
                details: JSON.parse(decodeURIComponent(options.details))
            });
            that.getMyLeaderInfo();
        }
        if (options.id) {
            that.setData({
                shareId: options.id
            });
            that.getData();
        }
        that.getBase();
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady: function() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow: function() {
        let that = this;
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide: function() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload: function() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh: function() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom: function() {},
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function() {},
    goIndex: function() {
        //返回首页
        wx.switchTab({
            url: "/pages/index/index"
        });
    },
    switchIndex: function(e) {
        //返回首页
        wx.switchTab({
            url: "/pages/index/index"
        });
    },
    //产品主数据-
    getData: function() {
        var that = this;
        var shareId = that.data.shareId;
        common.request({
            //endpoint: 'product/product_info',
            endpoint: "product/product_detail",
            method: "post",
            data: {
                id: shareId,
                visit_type: 1
            },
            success: res => {
                let details = res.extra;
                that.setData({
                    details: details
                });
                that.getMyLeaderInfo();
            },
            fail(err) {
                if (err.code == 1) {
                    wx.showModal({
                        title: "提示",
                        content: err.message,
                        confirmText: "返回首页",
                        showCancel: false,
                        success() {
                            wx.reLaunch({
                                url: "/pages/index/index"
                            });
                        }
                    });
                }
            }
        });
    },
    getBase: function() {
        var that = this;
        common.request({
            endpoint: "set/app_base",
            method: "get",
            data: {},
            success: res => {
                let wxapp_img = res.extra.wxapp_img ? res.extra.wxapp_img : "";
                if (wxapp_img.indexOf("https:") < 0) {
                    wxapp_img = wxapp_img.replace("http:", "https:");
                }
                that.setData({
                    wxapp_img: wxapp_img,
                    leaderName: res.extra.leader_title ? res.extra.leader_title : "团长",
                    title: res.extra.title ? res.extra.title : "社区团购"
                });
            }
        });
    },
    //获取团长信息
    getMyLeaderInfo: function() {
        let that = this;
        common.request({
            endpoint: "leader/myleaderinfo",
            method: "post",
            data: {
                openid: wx.getStorageSync("user").openid
            },
            success: res => {
                console.log(res.extra, "tuanzhang");
                that.setData({
                    myleaderinfo: res.extra
                });
                that.makeposter();
            }
        });
    },
    makeWxapp_img: function(ctx, options) {
        let that = this;
        if (that.data.wxapp_img) {
            wx.getImageInfo({
                src: that.data.wxapp_img,
                success: function(res) {
                    ctx.drawImage(res.path, 120 * that.data.rpx, 40 * that.data.rpx, 32.5 * that.data.rpx, 32.5 * that.data.rpx);
                    options.callback();
                },
                fail(res) {
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: "图片获取失败,图片地址为" + that.data.wxapp_img,
                        showCancel: false
                    });
                }
            });
        } else {
            options.callback();
        }
    },
    makeWxapp_img1: function(ctx, options) {
        let that = this;
        if (that.data.wxapp_img) {
            wx.getImageInfo({
                src: that.data.wxapp_img,
                success: function(res) {
                    ctx.drawImage(res.path, 120 * that.data.rpx, 30 * that.data.rpx, 32.5 * that.data.rpx, 32.5 * that.data.rpx);
                    options.callback();
                },
                fail(res) {
                    wx.hideLoading();
                    wx.showModal({
                        title: "提示",
                        content: "图片获取失败,图片地址为" + that.data.wxapp_img,
                        showCancel: false
                    });
                }
            });
        } else {
            options.callback();
        }
    },
    makeposter: function() {
        let that = this;
        var my_leaderID = that.data.myleaderinfo.leader_id;
        let leaderinfo_other = that.data.myleaderinfo.title ? that.data.myleaderinfo.title : "-";
        //社区
                let leaderinfo_name = that.data.myleaderinfo.name ? that.data.myleaderinfo.name : "-";
        let leaderinfo_title = that.data.myleaderinfo.other ? that.data.myleaderinfo.other : "-";
        //详细地址
                let leaderinfo_img = that.data.myleaderinfo.other ? that.data.myleaderinfo.img : "/images/default.png";
        const ctx = wx.createCanvasContext("firstCanvas");
        common.request({
            endpoint: "share/poster_read",
            method: "post",
            data: {
                lid: my_leaderID,
                id: that.data.details.id
            },
            success: res => {
                var res = JSON.parse(res);
                let poster_normal = res.data.poster_normal;
                if (poster_normal.indexOf("https:") < 0) {
                    poster_normal = poster_normal.replace("http:", "https:");
                }
                let icon = that.data.details.icon;
                if (icon.indexOf("https:") < 0) {
                    icon = icon.replace("http:", "https:");
                }
                let qc = res.data.qc;
                if (poster_normal.indexOf("ngoods_bg") != -1) {
                    wx.showLoading({
                        title: "海报生成中！"
                    });
                    that.setData({
                        killPoster: true
                    });
                    let type = poster_normal.substring(poster_normal.indexOf("ngoods_bg") + 9, poster_normal.indexOf(".jpg"));
                    wx.getImageInfo({
                        src: poster_normal,
                        success(res) {
                            ctx.drawImage(res.path, 0, 0, 375 * that.data.rpx, 667 * that.data.rpx);
                            if (type == 1) {
                                ctx.drawImage(that.data.domain + "/img/mainPack/type1_title.png", 105 * that.data.rpx, 52 * that.data.rpx, 163.5 * that.data.rpx, 18 * that.data.rpx);
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 22 * that.data.rpx, 110 * that.data.rpx, 330 * that.data.rpx, 330 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 18px sans-serif",
                                            FillStyle: "#333",
                                            word: that.data.details.name,
                                            maxWidth: 330 * that.data.rpx,
                                            x: 25 * that.data.rpx,
                                            y: 450 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal 18px";
                                        ctx.fillText("￥", 25 * that.data.rpx, 500 * that.data.rpx);
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 20px sans-serif";
                                        ctx.fillText(that.data.details.low_price, 40 * that.data.rpx, 500 * that.data.rpx);
                                        ctx.drawImage(that.data.domain + "/img/mainPack/low_price.png", 43 * that.data.rpx + ctx.measureText(that.data.details.low_price).width, 480 * that.data.rpx, 68 * that.data.rpx, 20.5 * that.data.rpx);
                                        ctx.setFillStyle("#969696");
                                        ctx.font = "normal bold 10px sans-serif";
                                        ctx.fillText("￥", 25 * that.data.rpx, 520 * that.data.rpx);
                                        ctx.setFillStyle("#969696");
                                        ctx.font = "normal bold 12px sans-serif";
                                        ctx.fillText(that.data.details.low_oprice, 35 * that.data.rpx, 520 * that.data.rpx);
                                        ctx.beginPath();
                                        ctx.setStrokeStyle("#969696");
                                        ctx.setLineWidth(1.5 * that.data.rpx);
                                        ctx.moveTo(25 * that.data.rpx, 516 * that.data.rpx);
                                        ctx.lineTo(35 * that.data.rpx + ctx.measureText(that.data.details.low_oprice).width, 516 * that.data.rpx);
                                        ctx.stroke();
                                        ctx.closePath();
                                        ctx.draw(true);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 13px sans-serif",
                                            FillStyle: "#fff",
                                            word: leaderinfo_name + "-" + leaderinfo_other,
                                            maxWidth: 200 * that.data.rpx,
                                            x: 25 * that.data.rpx,
                                            y: 570 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.drawImage(that.data.domain + "/img/mainPack/address_icon.png", 22 * that.data.rpx, 597 * that.data.rpx, 9 * that.data.rpx, 11 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "11px sans-serif",
                                            FillStyle: "#fff",
                                            word: leaderinfo_title,
                                            maxWidth: 200 * that.data.rpx,
                                            x: 35 * that.data.rpx,
                                            y: 590 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                ctx.arc(305 * that.data.rpx, 605 * that.data.rpx, 45 * that.data.rpx, 0, 2 * Math.PI);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 260 * that.data.rpx, 560 * that.data.rpx, 90 * that.data.rpx, 90 * that.data.rpx);
                                                ctx.draw(true, () => {
                                                    that.canvasToImage(ctx);
                                                });
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            } else if (type == 2) {
                                that.makeWxapp_img(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 55 * that.data.rpx);
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 9px sans-serif";
                                        ctx.fillText("实惠团购  好物尽享", 160 * that.data.rpx, 72 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 15 * that.data.rpx, 80 * that.data.rpx, 345 * that.data.rpx, 345 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 21px sans-serif",
                                            FillStyle: "#333",
                                            word: that.data.details.name,
                                            maxWidth: 330 * that.data.rpx,
                                            x: 20 * that.data.rpx,
                                            y: 460 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.drawImage(that.data.domain + "/img/mainPack/rect.png", 15 * that.data.rpx, 500 * that.data.rpx, 345 * that.data.rpx, 150 * that.data.rpx);
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                ctx.arc(75 * that.data.rpx, 565 * that.data.rpx, 50 * that.data.rpx, 0, 2 * Math.PI);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 25 * that.data.rpx, 515 * that.data.rpx, 100 * that.data.rpx, 100 * that.data.rpx);
                                                ctx.setFillStyle("#e93d3d");
                                                ctx.font = "normal bold 14px sans-serif";
                                                ctx.fillText("按住识别", 50 * that.data.rpx, 630 * that.data.rpx);
                                                ctx.setFillStyle("#e93d3d");
                                                ctx.font = "normal bold 20px sans-serif";
                                                ctx.fillText("抢购价:￥", 150 * that.data.rpx, 540 * that.data.rpx);
                                                ctx.setFillStyle("#e93d3d");
                                                ctx.font = "normal bold 24px sans-serif";
                                                ctx.fillText(that.data.details.low_price, 240 * that.data.rpx, 540 * that.data.rpx);
                                                ctx.setFillStyle("#333");
                                                ctx.font = "normal bold 15px sans-serif";
                                                ctx.fillText("团长：" + leaderinfo_name, 150 * that.data.rpx, 567 * that.data.rpx);
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "normal bold 15px sans-serif",
                                                    FillStyle: "#333",
                                                    word: "社区：" + leaderinfo_other,
                                                    maxWidth: 190 * that.data.rpx,
                                                    x: 150 * that.data.rpx,
                                                    y: 577 * that.data.rpx,
                                                    maxLine: 1
                                                });
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "normal bold 15px sans-serif",
                                                    FillStyle: "#333",
                                                    word: "地址：" + leaderinfo_title,
                                                    maxWidth: 190 * that.data.rpx,
                                                    x: 150 * that.data.rpx,
                                                    y: 597 * that.data.rpx,
                                                    maxLine: 2
                                                });
                                                ctx.draw(true, () => {
                                                    that.canvasToImage(ctx);
                                                });
                                            },
                                            fail(res) {
                                                wx.hideLoading();
                                                wx.showModal({
                                                    title: "提示",
                                                    content: "图片获取失败,图片地址为" + qc,
                                                    showCancel: false
                                                });
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            } else if (type == 3) {
                                that.makeWxapp_img1(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 45 * that.data.rpx);
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 9px sans-serif";
                                        ctx.fillText("实惠团购  好物尽享", 160 * that.data.rpx, 62 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 12 * that.data.rpx, 70 * that.data.rpx, 350 * that.data.rpx, 350 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 16px sans-serif",
                                            FillStyle: "#333",
                                            word: that.data.details.name,
                                            maxWidth: 350 * that.data.rpx,
                                            x: 22 * that.data.rpx,
                                            y: 430 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 16px sans-serif";
                                        ctx.fillText("￥", 22 * that.data.rpx, 480 * that.data.rpx);
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 18px sans-serif";
                                        ctx.fillText(that.data.details.low_price, 38 * that.data.rpx, 480 * that.data.rpx);
                                        var low_price_width = ctx.measureText(that.data.details.low_price).width;
                                        ctx.setFillStyle("#969696");
                                        ctx.font = "normal bold 12px sans-serif";
                                        ctx.fillText("￥", 40 * that.data.rpx + low_price_width, 480 * that.data.rpx);
                                        ctx.setFillStyle("#969696");
                                        ctx.font = "normal bold 14px sans-serif";
                                        ctx.fillText(that.data.details.low_oprice, 52 * that.data.rpx + low_price_width, 480 * that.data.rpx);
                                        var low_oprice_width = ctx.measureText(that.data.details.low_oprice).width;
                                        ctx.beginPath();
                                        ctx.setStrokeStyle("#969696");
                                        ctx.setLineWidth(1.5 * that.data.rpx);
                                        ctx.moveTo(52 * that.data.rpx + low_price_width, 475 * that.data.rpx);
                                        ctx.lineTo(52 * that.data.rpx + low_price_width + low_oprice_width, 475 * that.data.rpx);
                                        ctx.stroke();
                                        ctx.closePath();
                                        ctx.draw(true);
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "normal bold 15px sans-serif",
                                                    FillStyle: "#d0ffdb",
                                                    word: that.data.leaderName + "：" + leaderinfo_name + "-" + leaderinfo_other,
                                                    maxWidth: 250 * that.data.rpx,
                                                    x: 22 * that.data.rpx,
                                                    y: 570 * that.data.rpx,
                                                    maxLine: 1
                                                });
                                                ctx.drawImage(that.data.domain + "/img/mainPack/address_icon_blue.png", 22 * that.data.rpx, 602 * that.data.rpx, 9 * that.data.rpx, 11 * that.data.rpx);
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "normal bold 11px sans-serif",
                                                    FillStyle: "#d0ffdb",
                                                    word: "自提地址：" + leaderinfo_title,
                                                    maxWidth: 220 * that.data.rpx,
                                                    x: 35 * that.data.rpx,
                                                    y: 595 * that.data.rpx,
                                                    maxLine: 2
                                                });
                                                ctx.arc(325 * that.data.rpx, 605 * that.data.rpx, 45 * that.data.rpx, 0, 2 * Math.PI);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 280 * that.data.rpx, 560 * that.data.rpx, 90 * that.data.rpx, 90 * that.data.rpx);
                                                ctx.draw(true, () => {
                                                    that.canvasToImage(ctx);
                                                });
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            } else if (type == 4) {
                                that.makeWxapp_img(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 55 * that.data.rpx);
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 9px sans-serif";
                                        ctx.fillText("实惠团购  好物尽享", 160 * that.data.rpx, 72 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 12 * that.data.rpx, 100 * that.data.rpx, 350 * that.data.rpx, 380 * that.data.rpx);
                                        ctx.drawImage(that.data.domain + "/img/mainPack/detail.png", 12 * that.data.rpx, 430 * that.data.rpx, 350 * that.data.rpx, 50 * that.data.rpx);
                                        ctx.setFillStyle("#fff");
                                        ctx.font = "normal bold 16px sans-serif";
                                        ctx.fillText("￥", 20 * that.data.rpx, 460 * that.data.rpx);
                                        ctx.setFillStyle("#fff");
                                        ctx.font = "normal bold 18px sans-serif";
                                        ctx.fillText(that.data.details.low_price, 36 * that.data.rpx, 460 * that.data.rpx);
                                        var low_price_width = ctx.measureText(that.data.details.low_price).width;
                                        // ctx.setFillStyle("#fff")
                                        // ctx.font = "13px sans-serif"
                                        // ctx.fillText('原价', 45 * that.data.rpx + low_price_width, 460 * that.data.rpx);
                                        // ctx.setFillStyle("#fff")
                                        // ctx.font = "11px sans-serif"
                                        // ctx.fillText('￥', 72 * that.data.rpx + low_price_width, 460 * that.data.rpx);
                                                                                ctx.setFillStyle("#fff");
                                        ctx.font = "13px sans-serif";
                                        ctx.fillText(that.data.details.low_oprice, 85 * that.data.rpx + low_price_width, 460 * that.data.rpx);
                                        var low_oprice_width = ctx.measureText(that.data.details.low_oprice).width;
                                        ctx.beginPath();
                                        ctx.setStrokeStyle("#fff");
                                        ctx.setLineWidth(1.5 * that.data.rpx);
                                        ctx.moveTo(72 * that.data.rpx + low_price_width, 455 * that.data.rpx);
                                        ctx.lineTo(85 * that.data.rpx + low_price_width + low_oprice_width, 455 * that.data.rpx);
                                        ctx.stroke();
                                        ctx.closePath();
                                        ctx.draw(true);
                                        ctx.setFillStyle("#f75351");
                                        ctx.font = "14px sans-serif";
                                        ctx.fillText("已售" + that.data.details.order_num + "份", 280 * that.data.rpx, 470 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 16px sans-serif",
                                            FillStyle: "#232323",
                                            word: that.data.details.name,
                                            maxWidth: 340 * that.data.rpx,
                                            x: 20 * that.data.rpx,
                                            y: 490 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                ctx.arc(305 * that.data.rpx, 605 * that.data.rpx, 45 * that.data.rpx, 0, 2 * Math.PI);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 260 * that.data.rpx, 560 * that.data.rpx, 90 * that.data.rpx, 90 * that.data.rpx);
                                                ctx.setFillStyle("#434343");
                                                ctx.font = "12px sans-serif";
                                                ctx.fillText(that.data.leaderName + "：" + leaderinfo_name, 62 * that.data.rpx, 590 * that.data.rpx);
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "12px sans-serif",
                                                    FillStyle: "#434343",
                                                    word: "自提地址：" + leaderinfo_title,
                                                    maxWidth: 180 * that.data.rpx,
                                                    x: 62 * that.data.rpx,
                                                    y: 595 * that.data.rpx,
                                                    maxLine: 2
                                                });
                                                if (leaderinfo_img.indexOf("/images/default.png") == -1) {
                                                    wx.getImageInfo({
                                                        src: leaderinfo_img,
                                                        success(res) {
                                                            ctx.save();
                                                            ctx.arc(32 * that.data.rpx, 596 * that.data.rpx, 16 * that.data.rpx, 0, 2 * Math.PI);
                                                            ctx.setFillStyle("#fff");
                                                            ctx.clip();
                                                            ctx.drawImage(res.path, 20 * that.data.rpx, 580 * that.data.rpx, 32 * that.data.rpx, 32 * that.data.rpx);
                                                            ctx.restore();
                                                            ctx.draw(true, () => {
                                                                that.canvasToImage(ctx);
                                                            });
                                                        }
                                                    });
                                                } else {
                                                    ctx.draw(true, () => {
                                                        that.canvasToImage(ctx);
                                                    });
                                                }
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            } else if (type == 5) {
                                ctx.drawImage(that.data.domain + "/img/mainPack/type5_title.png", 80 * that.data.rpx, 45 * that.data.rpx, 190.5 * that.data.rpx, 17 * that.data.rpx);
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 13 * that.data.rpx, 168 * that.data.rpx, 350 * that.data.rpx, 350 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 16px sans-serif",
                                            FillStyle: "#232323",
                                            word: that.data.details.name,
                                            maxWidth: 335 * that.data.rpx,
                                            x: 25 * that.data.rpx,
                                            y: 540 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 18px sans-serif";
                                        ctx.fillText("￥" + that.data.details.low_price, 25 * that.data.rpx, 600 * that.data.rpx);
                                        // ctx.setFillStyle("#969696")
                                        // ctx.font = "normal bold 14px sans-serif"
                                        // ctx.fillText('原价：￥' + that.data.details.low_oprice, 120 * that.data.rpx, 600 * that.data.rpx);
                                                                                var low_oprice_width = ctx.measureText(that.data.details.low_oprice).width;
                                        ctx.beginPath();
                                        ctx.setStrokeStyle("#969696");
                                        ctx.setLineWidth(1 * that.data.rpx);
                                        ctx.moveTo(175 * that.data.rpx, 595 * that.data.rpx);
                                        ctx.lineTo(175 * that.data.rpx + low_oprice_width, 595 * that.data.rpx);
                                        ctx.stroke();
                                        ctx.closePath();
                                        ctx.draw(true);
                                        // that.drawFill(13 * that.data.rpx, 70 * that.data.rpx, 345 * that.data.rpx, 95 * that.data.rpx, 47.5 * that.data.rpx, '#f36055', 'fill', ctx);
                                                                                ctx.setFillStyle("#fff");
                                        ctx.font = "11px bold";
                                        ctx.fillText(that.data.leaderName + "：" + leaderinfo_name, 70 * that.data.rpx, 110 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "11px sans-serif",
                                            FillStyle: "#fff",
                                            word: "自提地址：" + leaderinfo_title,
                                            maxWidth: 200 * that.data.rpx,
                                            x: 70 * that.data.rpx,
                                            y: 115 * that.data.rpx,
                                            maxLine: 2
                                        });
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                ctx.arc(315 * that.data.rpx, 115 * that.data.rpx, 40 * that.data.rpx, 0, 2 * Math.PI);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 275 * that.data.rpx, 75 * that.data.rpx, 80 * that.data.rpx, 80 * that.data.rpx);
                                                if (leaderinfo_img.indexOf("/images/default.png") == -1) {
                                                    wx.getImageInfo({
                                                        src: leaderinfo_img,
                                                        success(res) {
                                                            ctx.save();
                                                            ctx.arc(41 * that.data.rpx, 116 * that.data.rpx, 16 * that.data.rpx, 0, 2 * Math.PI);
                                                            ctx.setFillStyle("#f36055");
                                                            ctx.clip();
                                                            ctx.drawImage(res.path, 25 * that.data.rpx, 100 * that.data.rpx, 32 * that.data.rpx, 32 * that.data.rpx);
                                                            ctx.restore();
                                                            ctx.draw(true, () => {
                                                                that.canvasToImage(ctx);
                                                            });
                                                        }
                                                    });
                                                } else {
                                                    ctx.draw(true, () => {
                                                        that.canvasToImage(ctx);
                                                    });
                                                }
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            } else if (type == 6) {
                                that.makeWxapp_img(ctx, {
                                    callback() {
                                        ctx.setFillStyle("#333");
                                        ctx.font = "normal bold 17px sans-serif";
                                        ctx.fillText(that.data.title, 160 * that.data.rpx, 53 * that.data.rpx);
                                        ctx.draw(true);
                                    }
                                });
                                wx.getImageInfo({
                                    src: icon,
                                    success(res) {
                                        ctx.drawImage(res.path, 37 * that.data.rpx, 100 * that.data.rpx, 300 * that.data.rpx, 300 * that.data.rpx);
                                        that.dealWords({
                                            ctx: ctx,
                                            font: "normal bold 18px sans-serif",
                                            FillStyle: "#232323",
                                            word: that.data.details.name,
                                            maxWidth: 335 * that.data.rpx,
                                            x: 37 * that.data.rpx,
                                            y: 410 * that.data.rpx,
                                            maxLine: 1
                                        });
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 16px sans-serif";
                                        ctx.fillText("￥", 37 * that.data.rpx, 470 * that.data.rpx);
                                        ctx.setFillStyle("#fe3112");
                                        ctx.font = "normal bold 18px sans-serif";
                                        ctx.fillText(that.data.details.low_price, 53 * that.data.rpx, 470 * that.data.rpx);
                                        var low_price_width = ctx.measureText(that.data.details.low_price).width;
                                        // ctx.setFillStyle("#969696")
                                        // ctx.font = "14px sans-serif"
                                        // ctx.fillText('原价:', 70 * that.data.rpx + low_price_width, 470 * that.data.rpx);
                                        // ctx.setFillStyle("#969696")
                                        // ctx.font = "12px sans-serif"
                                        // ctx.fillText('￥', 100 * that.data.rpx + low_price_width, 470 * that.data.rpx);
                                                                                ctx.setFillStyle("#969696");
                                        ctx.font = "14px sans-serif";
                                        ctx.fillText(that.data.details.low_oprice, 112 * that.data.rpx + low_price_width, 470 * that.data.rpx);
                                        var low_oprice_width = ctx.measureText(that.data.details.low_oprice).width;
                                        ctx.beginPath();
                                        ctx.setStrokeStyle("#969696");
                                        ctx.setLineWidth(1 * that.data.rpx);
                                        ctx.moveTo(100 * that.data.rpx + low_price_width, 465 * that.data.rpx);
                                        ctx.lineTo(112 * that.data.rpx + low_price_width + low_oprice_width, 465 * that.data.rpx);
                                        ctx.stroke();
                                        ctx.closePath();
                                        ctx.draw(true);
                                        wx.getImageInfo({
                                            src: qc,
                                            success(res) {
                                                ctx.rect(37 * that.data.rpx, 530 * that.data.rpx, 100 * that.data.rpx, 100 * that.data.rpx);
                                                ctx.setFillStyle("#fff");
                                                ctx.fill();
                                                ctx.drawImage(res.path, 43 * that.data.rpx, 540 * that.data.rpx, 90 * that.data.rpx, 90 * that.data.rpx);
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "normal bold 18px sans-serif",
                                                    FillStyle: "#434343",
                                                    word: leaderinfo_name + "-" + leaderinfo_other,
                                                    maxWidth: 180 * that.data.rpx,
                                                    x: 160 * that.data.rpx,
                                                    y: 540 * that.data.rpx,
                                                    maxLine: 1
                                                });
                                                ctx.drawImage(that.data.domain + "/img/mainPack/address_icon_red.png", 160 * that.data.rpx, 572 * that.data.rpx, 9 * that.data.rpx, 11 * that.data.rpx);
                                                that.dealWords({
                                                    ctx: ctx,
                                                    font: "14px sans-serif",
                                                    FillStyle: "#969696",
                                                    word: leaderinfo_title,
                                                    maxWidth: 180 * that.data.rpx,
                                                    x: 172 * that.data.rpx,
                                                    y: 565 * that.data.rpx,
                                                    maxLine: 1
                                                });
                                                that.drawFill(160 * that.data.rpx, 600 * that.data.rpx, 100 * that.data.rpx, 16 * that.data.rpx, 8 * that.data.rpx, "#ffd1cc", "fill", ctx);
                                                ctx.setFillStyle("#363636");
                                                ctx.font = "11px sans-serif";
                                                ctx.fillText("长按识别二维码", 168 * that.data.rpx, 613 * that.data.rpx);
                                                ctx.draw(true, () => {
                                                    that.canvasToImage(ctx);
                                                });
                                            }
                                        });
                                    },
                                    fail(res) {
                                        wx.hideLoading();
                                        wx.showModal({
                                            title: "提示",
                                            content: "图片获取失败,图片地址为" + icon,
                                            showCancel: false
                                        });
                                    }
                                });
                            }
                        },
                        fail(res) {
                            wx.hideLoading();
                            wx.showModal({
                                title: "提示",
                                content: "图片获取失败,图片地址为" + poster_normal,
                                showCancel: false
                            });
                        }
                    });
                } else {
                    //自定义
                    that.makeposter_define();
                }
            }
        });
    },
    /**
   * x,y-左上角点的坐标；width、height-宽高；radius-圆角；color-绘制颜色；type-绘制类型（'fill'和'stroke'）。
   */
    drawFill: function(x, y, width, height, radius, color, type, ctx) {
        ctx.beginPath();
        ctx.moveTo(x, y + radius);
        ctx.lineTo(x, y + height - radius);
        ctx.quadraticCurveTo(x, y + height, x + radius, y + height);
        ctx.lineTo(x + width - radius, y + height);
        ctx.quadraticCurveTo(x + width, y + height, x + width, y + height - radius);
        ctx.lineTo(x + width, y + radius);
        ctx.quadraticCurveTo(x + width, y, x + width - radius, y);
        ctx.lineTo(x + radius, y);
        ctx.quadraticCurveTo(x, y, x, y + radius);
        ctx[type + "Style"] = color || params.color;
        ctx.closePath();
        ctx[type]();
    },
    previewImage: function() {
        let that = this;
        wx.previewImage({
            current: "",
            // 当前显示图片的http链接
            urls: that.data.previewImageUrl
        });
    },
    goBack: function() {
        wx.navigateBack({});
    },
    canvasToImage: function() {
        let that = this;
        setTimeout(() => {
            wx.canvasToTempFilePath({
                x: 0,
                y: 0,
                width: 375 * that.data.rpx,
                height: 667 * that.data.rpx,
                destWidth: 4 * 375 * that.data.rpx,
                destHeight: 4 * 667 * that.data.rpx,
                canvasId: "firstCanvas",
                fileType: "png",
                quality: 0,
                success: function(res) {
                    wx.hideLoading();
                    let url = [];
                    url.push(res.tempFilePath);
                    that.setData({
                        previewImageUrl: url
                    });
                },
                fail: function(res) {},
                complete: function(res) {}
            }, that);
        }, 100);
    },
    //处理文字多出省略号显示
    dealWords: function(options) {
        options.ctx.setFontSize(options.fontSize);
        //设置字体大小
                options.ctx.font = options.font;
        options.ctx.setFillStyle(options.FillStyle);
        var allRow = Math.ceil(options.ctx.measureText(options.word).width / options.maxWidth);
        //实际总共能分多少行
                var count = allRow >= options.maxLine ? options.maxLine : allRow;
        //实际能分多少行与设置的最大显示行数比，谁小就用谁做循环次数
                var endPos = 0;
        //当前字符串的截断点
                for (var j = 0; j < count; j++) {
            var nowStr = options.word.slice(endPos);
            //当前剩余的字符串
                        var rowWid = 0;
            //每一行当前宽度    
                        if (options.ctx.measureText(nowStr).width > options.maxWidth) {
                //如果当前的字符串宽度大于最大宽度，然后开始截取
                for (var m = 0; m < nowStr.length; m++) {
                    rowWid += options.ctx.measureText(nowStr[m]).width;
                    //当前字符串总宽度
                                        if (rowWid > options.maxWidth) {
                        if (j === options.maxLine - 1) {
                            //如果是最后一行
                            options.ctx.fillText(nowStr.slice(0, m - 1) + "...", options.x, options.y + (j + 1) * 18);
                            //(j+1)*18这是每一行的高度        
                                                } else {
                            options.ctx.fillText(nowStr.slice(0, m), options.x, options.y + (j + 1) * 18);
                        }
                        endPos += m;
                        //下次截断点
                                                break;
                    }
                }
            } else {
                //如果当前的字符串宽度小于最大宽度就直接输出
                options.ctx.fillText(nowStr.slice(0), options.x, options.y + (j + 1) * 18);
            }
        }
    }
});